#!/usr/bin/python3
# -*- encoding: utf-8 -*-
# @File: windows_key.py
# @Author: SunPengfei
# @Date: 2020/08/20 09:59:33
# @Contact: sunpengfei@sczq.com.cn
# @Copyright: Capital Securities Co., Ltd
# @Desc: Window下模拟发送Key按键


from key_const import VK_CODE, multi_key
from collections.abc import Iterable
import win32api as w3a


def send_key(key):
    """
    发送单个按键
    :params key: 需要发送键
    """
    w3a.keybd_event(key, 0, 0, 0)
    w3a.keybd_event(key, 0, 2, 0)


def send_multi_key(key, key1):
    """
    数日组合键，支持二键组合
    :params key: 需要发送键
    :params key1: 需要发送键
    """
    w3a.keybd_event(VK_CODE.get(key), 0, 0, 0)
    send_key(VK_CODE.get(key1))
    w3a.keybd_event(VK_CODE.get(key), 0, 2, 0)


def inputs(keys):
    """
    传入一个字符串，则会按照每个字符输入
    :params keys: 需要发送键的集合，可以是可迭代对象的密码，但最终一拿到的对象一定单个的字符
    """
    if isinstance(keys, str) and len(keys) == 1:
        if VK_CODE.get(keys):
            send_key(VK_CODE[keys])
        elif multi_key.get(keys):
            send_multi_key(*multi_key[keys])
        else:
            raise "请确认按键"
    elif isinstance(keys, Iterable):
        for i in keys:
            inputs(i)


if __name__ == "__main__":
    keys = "12323ASDASSD"
    inputs(keys)
